from helpers.director.shortcut import page_dc
from .models import QualityEvent,FailAbstract,FailKind,ProcessingScheme
from helpers.director.model_func.dictfy import sim_dict
import json

class PrintEvent(object):
    def __init__(self, request,*args, **kwargs):
        self.requests = request
    
    def get_template(self):
        return 'director/web.html'
    
    def get_context(self):
        pp= self.requests.GET.get('id')
        e = QualityEvent .objects.get(pk = pp)
        abstract_options_dict = {x.pk:str(x) for x in FailAbstract.objects.all() }
        fail_count= json.loads(e.fail_content) if e.fail_content else []
        fail_kind_options_dict = {x.pk:str(x) for x in FailKind.objects.all()}
        process_kind_dc ={x.pk:str(x) for x in ProcessingScheme.objects.all()}
        for item in fail_count:
            item['_abstract_label'] = abstract_options_dict.get(item.get('abstract'))
            if item.get('fail_kind'):
                item['_fail_kind_label'] = fail_kind_options_dict.get(item.get('fail_kind'))
            if item.get('process_kind'):
                item['_process_kind_label'] = process_kind_dc.get(item.get('process_kind'))
      
        inst_dc = sim_dict(e)
        return {
            'editor':'printEvent',
            'editor_ctx':{
                'prod_no':e.prod_no,
                'model_spec':e.model_spec,
                'model_name':e.model_name,
                'model_no':e.model_no,
                'fail_source':str( e.fail_source ),
                'fail_count':e.fail_count,
                'total_count':e.total_count,
                'create_time':e.create_time,
                'fail_content':fail_count,
                'validate_action':e.validate_action,
                'approval_suggest':e.approval_suggest,
                '_kind_label':inst_dc.get('_kind_label','')
                
            }
        }



page_dc.update({
    'print_event':PrintEvent
})